package com.xph.core.filter;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
	protected FilterConfig config;
	protected String encoding = null;

	public void destroy() {
		encoding = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		if (encoding == null) {
			encoding = "UTF-8";
		}
		response.setCharacterEncoding(encoding);
		request.setCharacterEncoding(encoding);
		response.setContentType("text/html;charset="+encoding);
		arg2.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		this.config = arg0;
		
		/*从web.xml里读取初始值*/
		this.encoding = config.getInitParameter("Encoding");
	}

}
